package com.yaodian100.ec.shopping.entity;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.yaodian100.core.common.entity.BaseEntity;
import com.yaodian100.ec.campaign.entity.CampaignProduct;
import com.yaodian100.ec.product.entity.AddOnProduct;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.Specification;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.entity.ShelfTuangou;

@Entity
@Table(name = "EC_CART_ORDER_DETAIL")
public class CartOrderProductDetail extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3074854730453620747L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_ec_cart_order_detail")
	@SequenceGenerator(name = "seq_ec_cart_order_detail", sequenceName = "seq_ec_cart_order_detail")
	@Column(name = "cart_order_detail_id")
	private Long cartOrderDetailId;

	@Column(name = "cart_order_prod_id", insertable = false, updatable = false)
	private Long cartOrderProdId;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "cart_order_prod_id", nullable = false)
	private CartOrderProduct orderProduct;

	@Column(name = "cart_id", length = 50, insertable = false, updatable = false)
	private String cartId;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "cart_id", nullable = false)
	private CartOrders cart;

	@Column(name = "prod_id", length = 10, nullable = false)
	private String prodId;

	@Column(name = "prod_spec_id", length = 10, nullable = false)
	private String prodSpecId;

	@Column(name = "prod_name", length = 500, nullable = false)
	private String prodName;

	@Column(name = "prod_spec_name", length = 300, nullable = false)
	private String prodSpecName;

	/** detailTp, 0主商品 1預購, 2贈品 , 3加購, 4活動, 5满额购, 6N選M */
	@Column(name = "detail_tp", length = 5)
	private String detailTp;

	@Column(name = "ec_cost", nullable = false, columnDefinition = "numeric(10,2)")
	private BigDecimal ecCost = BigDecimal.ZERO;

	@Column(name = "erp_cost", nullable = false, columnDefinition = "numeric(10,2)")
	private BigDecimal erpCost = BigDecimal.ZERO;

	@Column(name = "qty", nullable = false)
	private int qty = 0;

	/**
	 * 重量(g)
	 */
	@Column(name = "PRODUCT_WEIGHT", columnDefinition = "NUMERIC(10, 2)")
	private BigDecimal weight;

	/** amount, 商品單位售價 */
	@Column(name = "amount", nullable = false, columnDefinition = "numeric(10,2)")
	private BigDecimal amount = BigDecimal.ZERO;

	@Column(name = "vend_id", length = 15)
	private String vendId;

	@Column(name = "prod_seq", nullable = false, columnDefinition = "numeric(5)")
	private int prodSeq = 0;

	@Column(name = "detail_seq", nullable = false, columnDefinition = "numeric(5)")
	private int detailSeq = 0;

	/**
	 * 體積
	 */
	@Column(name = "PRODUCT_VOLUME", columnDefinition = "NUMERIC(10, 2)")
	private BigDecimal volume;

	/** default constructor */
	public CartOrderProductDetail() {
		super();
	}

	/** default constructor */
	public CartOrderProductDetail(Product prod, Specification spec, Shelf shelf, int qty, CampaignProduct cp) {
		super();
		this.prodId = prod.getProductId();
		this.prodName = prod.getProductName();
		this.weight = prod.getProductWeight();
		this.vendId = prod.getSupplierId();

		this.prodSpecId = spec.getSpecificationId().toString();
		this.prodSpecName = spec.getSpecificationName();
		this.qty = qty;
		this.ecCost = prod.getProductCost();

		this.amount = shelf.getShelfPrice();
		this.volume = prod.getProductVolume();
		if (cp != null) {
			// cp 不為 null, 表示滿額購, 商品售價減掉 cp.discount
			this.amount = cp.getPrice();
			this.detailTp = "5";
		} else {
			// 0主商品 1預購, 2贈品 , 3加購, 4活動, 5满额购, 6N選M
			if (prod.getProductType() == 6) {
				this.detailTp = "6";
			} else {
				this.detailTp = String.valueOf(prod.getProductType());
			}
		}
	}

	/** default constructor */
	public CartOrderProductDetail(AddOnProduct addOnProd, Product prod, Specification spec, int qty) {
		super();
		this.prodId = addOnProd.getAddOnProductId();

		this.prodName = prod.getProductName();
		this.weight = prod.getProductWeight();
		this.ecCost = prod.getProductCost();
		this.vendId = prod.getSupplierId();

		this.prodSpecId = spec.getSpecificationId().toString();
		this.prodSpecName = spec.getSpecificationName();
		this.qty = qty;
		this.amount = addOnProd.getAddOnPrice();
		this.volume = prod.getProductVolume();
		// addOn table 中價格為 0 表示贈品, 大於 0 為 加價購, 小於 0 為N選M 使用, 0主商品 1預購, 2贈品, 3加購, 4活動, 5满额购, 6N選M
		if (addOnProd.getAddOnPrice().floatValue() == 0.0f) {
			this.detailTp = "2"; // 赠品
		} else if (addOnProd.getAddOnPrice().floatValue() < 0.0f) {
			this.detailTp = "6"; // N 选 M
			this.amount = BigDecimal.ZERO;
		} else {
			this.detailTp = "3"; // 加价购
		}
	}

	public CartOrderProductDetail(Product prod, Specification spec, ShelfTuangou shelfTuangou, int qty) {
		super();
		this.prodId = prod.getProductId();
		this.prodName = prod.getProductName();
		this.weight = prod.getProductWeight();
		this.vendId = prod.getSupplierId();

		this.prodSpecId = spec.getSpecificationId().toString();
		this.prodSpecName = spec.getSpecificationName();
		this.qty = qty;
		this.ecCost = prod.getProductCost();
		this.amount = shelfTuangou.getPrice();
		this.volume = prod.getProductVolume();
		// 0主商品 1預購, 7 限立即購買商品
		this.detailTp = String.valueOf(prod.getProductType());
	}

	public int getProdSeq() {
		if (prodSeq == 0) {
			prodSeq = orderProduct.getProdSeq();
		}
		return prodSeq;
	}

	public void setProdSeq(int prodSeq) {
		this.prodSeq = prodSeq;
	}

	public int getDetailSeq() {
		if (detailSeq == 0) {
			detailSeq = orderProduct.getDetails().indexOf(this) + 1;
		}
		return detailSeq;
	}

	public void setDetailSeq(int detailSeq) {
		this.detailSeq = detailSeq;
	}

	/**
	 * @return the weight
	 */
	public BigDecimal getWeight() {
		return weight;
	}

	/**
	 * @param weight the weight to set
	 */
	public void setWeight(BigDecimal weight) {
		this.weight = weight;
	}

	/**
	 * @return the cart
	 */
	public CartOrders getCart() {
		return cart;
	}

	/**
	 * @param cart the cart to set
	 */
	public void setCart(CartOrders cart) {
		this.cart = cart;
	}

	/**
	 * @return the orderProduct
	 */
	public CartOrderProduct getOrderProduct() {
		return orderProduct;
	}

	/**
	 * @param orderProduct the orderProduct to set
	 */
	public void setOrderProduct(CartOrderProduct orderProduct) {
		this.orderProduct = orderProduct;
	}

	public String getProdId() {
		return prodId;
	}

	public void setProdId(String prodId) {
		this.prodId = prodId;
	}

	public String getProdSpecId() {
		return prodSpecId;
	}

	public void setProdSpecId(String prodSpecId) {
		this.prodSpecId = prodSpecId;
	}

	public String getProdName() {
		return prodName;
	}

	public void setProdName(String prodName) {
		this.prodName = prodName;
	}

	public String getProdSpecName() {
		return prodSpecName;
	}

	public void setProdSpecName(String prodSpecName) {
		this.prodSpecName = prodSpecName;
	}

	public String getDetailTp() {
		return detailTp;
	}

	public void setDetailTp(String detailTp) {
		this.detailTp = detailTp;
	}

	public BigDecimal getEcCost() {
		return ecCost;
	}

	public void setEcCost(BigDecimal ecCost) {
		this.ecCost = ecCost;
	}

	public BigDecimal getErpCost() {
		return erpCost;
	}

	public void setErpCost(BigDecimal erpCost) {
		this.erpCost = erpCost;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public String getVendId() {
		return vendId;
	}

	public void setVendId(String vendId) {
		this.vendId = vendId;
	}

	/**
	 * @return the cartOrderDetailId
	 */
	public Long getCartOrderDetailId() {
		return cartOrderDetailId;
	}

	/**
	 * @param cartOrderDetailId the cartOrderDetailId to set
	 */
	public void setCartOrderDetailId(Long cartOrderDetailId) {
		this.cartOrderDetailId = cartOrderDetailId;
	}

	/**
	 * @return the cartId
	 */
	public String getCartId() {
		return cartId;
	}

	/**
	 * @param cartId the cartId to set
	 */
	public void setCartId(String cartId) {
		this.cartId = cartId;
	}

	/**
	 * @return the cartOrderProdId
	 */
	public Long getCartOrderProdId() {
		return cartOrderProdId;
	}

	/**
	 * @param cartOrderProdId the cartOrderProdId to set
	 */
	public void setCartOrderProdId(Long cartOrderProdId) {
		this.cartOrderProdId = cartOrderProdId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "CartOrderProductDetail [cartId=" + cartId + ", prodId=" + prodId + ", prodSpecId=" + prodSpecId
				+ ", detailTp=" + detailTp + ", qty=" + qty + ", amount=" + amount + "]";
	}

	public BigDecimal getVolume() {
		return volume;
	}

	public void setVolume(BigDecimal volume) {
		this.volume = volume;
	}

}
